package com.cloud.class05.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.cloud.class05.entities.Pay;
import com.cloud.class05.resp.ResultData;
import com.cloud.class05.service.PayService;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;

@RestController
//@Controller
//@ResponseBody
public class PayGateWayController {
    @Autowired
    public PayService payService;

    @GetMapping(value = "/pay/gateway/get/{id}")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        Pay pay = payService.getById(id);
        return ResultData.success(pay);
    }

    @GetMapping(value = "/pay/gateway/info")
    public ResultData<String> getGateWayInfo() {
        return ResultData.success("gateway info test \t" + IdUtil.simpleUUID());
    }

    @GetMapping(value = "/pay/gateway/filter")
    public ResultData<String> getGateWayFilter(HttpServletRequest request) {
        String result = "";
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headName = headerNames.nextElement();
            String headValue = request.getHeader(headName);
            System.out.println("请求头名：" + headName + "，请求头值--->" + headValue);
            if (headName.equalsIgnoreCase("X-Request-atguigu1")
                    || headName.equalsIgnoreCase("X-Request-atguigu2")) {
                result = result + headName + "\t " + headValue + " ";
            }
        }
        System.out.println("=======================================");
        String customerId = request.getParameter("customerId");
        System.out.println("请求参数customerId：" + customerId);
        String customerName = request.getParameter("customerName");
        System.out.println("请求参数customerName：" + customerName);
        System.out.println("=======================================");

        return ResultData.success("getGatewayFilter 过滤器 test： " + result + " \t " + DateUtil.now());
    }
}
